package com.xcl.bili.service;

import com.mysql.cj.util.StringUtils;
import com.xcl.bili.dao.UserDao;
import com.xcl.bili.domain.User;
import com.xcl.bili.domain.UserInfo;
import com.xcl.bili.domain.constant.UserConstant;
import com.xcl.bili.domain.exception.ConditionException;
import com.xcl.bili.service.utils.MD5Util;
import com.xcl.bili.service.utils.RSAUtil;
import com.xcl.bili.service.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public  void  addUser(User user){
        String phone = user.getPhone();
        //1. 检查手机号是否为空;
        //2. 手机号是否注册;
        //3. 密码rsa解密, md5加盐;
        //4. 保存用户, 用户信息数据; 使用常量存储默认名字;

        if(StringUtils.isNullOrEmpty(phone)){
            throw  new ConditionException("手机号不能为空");
        }

        User dbUser = getUserByPhone(phone);
        if(dbUser!=null){
            throw new ConditionException("该手机号已经注册");
        }

        Date now = new Date();
        String salt = String.valueOf(now);
        String password = user.getPassword();
        String rawPassword;
        try {
            rawPassword = RSAUtil.decrypt(password);
        }catch (Exception e){
            throw new ConditionException("密码解密失败");
        }
        String md5Password = MD5Util.sign(rawPassword, salt, "UTF-8");
        user.setSalt(salt);
        user.setPassword(md5Password);
        user.setCreateTime(now);
        userDao.addUser(user);

        //添加用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setNick(UserConstant.DEFAULT_NICK);
        userInfo.setBirth(UserConstant.DEFAULT_BIRTH);
        userInfo.setGender(UserConstant.GENDER_MAIL);
        userInfo.setCreateTime(now);
        userDao.addUserInfo(userInfo);


    }

    private User getUserByPhone(String phone) {

        return userDao.getUserByPhone(phone);
    }


    public String login(User user) throws Exception {

        String phone = user.getPhone();
        //1. 检查手机号是否为空;
        //2. 检查用户是否存在;
        //3. 比对传入和数据库的密码;
        //4. 生成token;

        if(StringUtils.isNullOrEmpty(phone)){
            throw  new ConditionException("手机号不能为空");
        }

        User dbUser = getUserByPhone(phone);
        if(dbUser == null){
            throw new ConditionException("该手机号未注册");
        }
        String rawPassword;
        try {
            rawPassword = RSAUtil.decrypt(user.getPassword());
        }catch (Exception e){
            throw new ConditionException("密码解密失败");
        }
        String md5Password = MD5Util.sign(rawPassword,dbUser.getSalt(),"UTF-8");
        if(!md5Password.equals(dbUser.getPassword())){
            throw new ConditionException("密码不对");
        }

        return TokenUtil.generateToken(dbUser.getId());
    }
}
